package com.zuoye.Dao;

import com.zuoye.Opjo.Class;
import com.zuoye.Opjo.Student;
import com.zuoye.UBtil.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/30  16:54
 * @Copyright
 */
public class StuDaoIntImpl implements StuDaoIntrface {
    @Override
    public int insert(ArrayList<Student> list) {
        String sql = "insert into stu(id,name,age,height,sex,birthday,cla_id) values (?,?, ?, ?, ?, ?, ?)";
        try (final Connection connection = DbUtil.getConnection();
             final PreparedStatement preparedStatement = connection.prepareStatement(sql);) {
            int i = 0;
            Iterator<Student> iterator = list.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                preparedStatement.setInt(1, student.getId());
                preparedStatement.setString(2, student.getName());
                preparedStatement.setInt(3, student.getAge());
                preparedStatement.setDouble(4, student.getHeight());
                preparedStatement.setString(5, student.getSex());
                preparedStatement.setString(6, student.getBirthday());
                preparedStatement.setInt(7, student.getCla_id());
                i = preparedStatement.executeUpdate();
            }
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int studentId) {
        String sql = "delete from stu where id = ?";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, studentId);
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Student student) {
        String sql = "update stu set name=?,age=?,height=?,sex=?,birthday=?,cla_id=? where id = ?";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            int i = 0;
            preparedStatement.setString(1, student.getName());
            preparedStatement.setInt(2, student.getAge());
            preparedStatement.setDouble(3, student.getHeight());
            preparedStatement.setString(4, student.getSex());
            preparedStatement.setString(5, student.getBirthday());
            preparedStatement.setInt(6, student.getCla_id());
            preparedStatement.setInt(7, student.getId());
            i = preparedStatement.executeUpdate();
            System.out.println("i = " + i);
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public ArrayList<Student> query(Student student) {
        final ArrayList<Student> list = new ArrayList<>();
        try (final Connection connection = DbUtil.getConnection();
             final PreparedStatement preparedStatement = connection.prepareStatement("select * from stu");
             final ResultSet resultSet = preparedStatement.executeQuery();) {
            while (resultSet.next()) {
                final Student student1 = new Student();
                student1.setId(resultSet.getInt("id"));
                student1.setAge(resultSet.getInt("age"));
                student1.setHeight(resultSet.getDouble("height"));
                student1.setSex(resultSet.getString("sex"));
                student1.setBirthday(resultSet.getString("birthday"));
                student1.setCla_id(resultSet.getInt("cla_id"));
                list.add(student1);
            }
            System.out.println("list = " + list);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int transaction(Connection connection, ArrayList<Student> list) {
        String sql = "insert into stu(name,age,height,sex,birthday,cla_id)values (?, ?, ?, ?, ?, ?)";
        try (final PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            Iterator<Student> iterator = list.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                Student student = iterator.next();
                preparedStatement.setString(1, student.getName());
                preparedStatement.setInt(2, student.getAge());
                preparedStatement.setDouble(3, student.getHeight());
                preparedStatement.setString(4, student.getSex());
                preparedStatement.setString(5, student.getBirthday());
                preparedStatement.setInt(6, student.getCla_id());
                i = preparedStatement.executeUpdate();
                System.out.println("i " + i);
            }
            return i;
        } catch (SQLException throwables) {
            throw new RuntimeException("插入学习异常");
        }

    }

}
